public class Conditions {

	private Statements then;
	private Statements not;
	private Condition condition;

	public Conditions(Tokens tokens) {
		this.not = null;
		this.parse(tokens);
	}
		
	public void parse(Tokens tokens) {
		// check for "if"
		if(tokens.getToken() != 5){
			System.err.println("Expected keyword \"if\" before token: " + tokens.idName());
			System.exit(0);
		}
		tokens.skipToken();
		this.condition = new Condition(tokens);
		// check for "then"
		if(tokens.getToken() != 6){
			System.err.println("Expected keyword \"then\" before token: " + tokens.idName());
			System.exit(0);
		}
		tokens.skipToken();
		this.then = new Statements(tokens);
		// check for "else"
		if(tokens.getToken() == 7){
			tokens.skipToken();
			this.not = new Statements(tokens);
		}
		if (tokens.getToken() != 3) {
			System.err.println("If statements must end with keyword \"end\"");
			System.exit(0);
		} else {
			tokens.skipToken();
		}
		if (tokens.getToken() != 12) {
			System.err.println("If statements must end with an \"end\" followed by a semicolon");
			System.exit(0);
		} else {
			tokens.skipToken();
		}
	}
	
	
	/**
	 * Runs the If statement. If condition returns true, executes the then statement.
	 * If it returns false, executes the else (or nothing if else statement is empty)
	 */
	public void runIf() {
		boolean flag = this.condition.runCond();
		if(flag){
			this.then.runStmtSeq();
		} else if(!flag && not != null){
			this.not.runStmtSeq();
		}
	}
}